package com.growthsee.rider.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.growthsee.common.core.controller.BaseController;
import com.growthsee.common.core.domain.AjaxResult;
import com.growthsee.common.core.page.PageDomain;
import com.growthsee.common.core.page.TableDataInfo;
import com.growthsee.common.utils.SecurityUtils;
import com.growthsee.common.utils.bean.BeanUtils;
import com.growthsee.rider.domain.RiderCompany;
import com.growthsee.rider.domain.dto.RiderCompanyDto;
import com.growthsee.rider.enumeration.StatusEnum;
import com.growthsee.rider.enumeration.YesNoEnum;
import com.growthsee.rider.param.BaseParam;
import com.growthsee.rider.param.CompanyParam;
import com.growthsee.rider.service.RiderCompanyService;
import com.growthsee.rider.utils.BeanUtilsExt;
import com.growthsee.rider.wrapper.CompanyWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author csy
 * @since 2021-05-17
 */
@AllArgsConstructor
@RestController
@RequestMapping("/rider/company")
@Api("骑手公司管理")
public class RiderCompanyController extends BaseController {
    private final RiderCompanyService riderCompanyService;

    @ApiOperation("分页查询骑手公司")
    @GetMapping
    public TableDataInfo query(PageDomain page, BaseParam baseParam) {
        LambdaQueryWrapper<RiderCompany> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RiderCompany::getDeleted, YesNoEnum.NO.getValue());
        if(StringUtils.isNotEmpty(baseParam.getKeywords())){
            queryWrapper.like(RiderCompany::getName, baseParam.getKeywords());
        }
        Page page1 = riderCompanyService.page(new Page<>(page.getPageNum(),page.getPageSize()), queryWrapper);
        val dtoPage = BeanUtilsExt.copyPageData(page1, RiderCompanyDto.class);
        CompanyWrapper.builder().dataHandler(dtoPage.getRecords());
        return getDataTable(dtoPage.getRecords(),dtoPage.getTotal());
    }
    @ApiOperation("添加骑手公司")
    @PostMapping
    @ApiImplicitParam(name = "riderCompany", value = "公司信息", dataType = "RiderCompany")
    public AjaxResult insert(@RequestBody RiderCompany riderCompany) {
        String checkResult = CompanyWrapper.builder().check(riderCompany);
        if(StringUtils.isNotEmpty(checkResult))return AjaxResult.error(checkResult);
        riderCompany.setCreateBy(SecurityUtils.getUsername());
        riderCompany.setCreateTime(new Date());
        riderCompany.setDeleted(YesNoEnum.NO.getValue());
        riderCompany.setStatus(StatusEnum.ENABLE.getValue());
        riderCompanyService.save(riderCompany);
        return AjaxResult.success();
    }
    @ApiImplicitParam(name = "riderCompany", value = "公司信息", dataType = "RiderCompany")
    @ApiOperation("编辑骑手公司")
    @PutMapping
    public AjaxResult update(@RequestBody RiderCompany company) {
        String checkResult = CompanyWrapper.builder().check(company);
        if(StringUtils.isNotEmpty(checkResult))return AjaxResult.error(checkResult);
        company.setUpdateBy(SecurityUtils.getUsername());
        company.setUpdateTime(new Date());
        riderCompanyService.updateById(company);
        return AjaxResult.success();
    }
    @ApiOperation("根据id查询骑手公司")
    @GetMapping("{id}")
    public AjaxResult findOne(@PathVariable String id) {
        RiderCompany riderCompany = riderCompanyService.getById(id);
        RiderCompanyDto riderCompanyDto = new RiderCompanyDto();
        BeanUtils.copyBeanProp(riderCompanyDto,riderCompany);
        CompanyWrapper.builder().dataHandler(Arrays.asList(riderCompanyDto));
        return AjaxResult.success(riderCompanyDto);
    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "根据id删除公司", notes = "根据id删除公司")
    public AjaxResult deleteById(@PathVariable String id) {
        riderCompanyService.update(Wrappers.<RiderCompany>lambdaUpdate()
                .set(RiderCompany::getDeleted,YesNoEnum.YES.getValue())
                .eq(RiderCompany::getId,id));
        return AjaxResult.success();
    }

    @ApiOperation(value = "条件查询公司", notes = "条件查询公司")
    @GetMapping("param")
    public AjaxResult param(CompanyParam companyParam) {
        LambdaQueryWrapper<RiderCompany> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RiderCompany::getDeleted, YesNoEnum.NO.getValue());
        if(StringUtils.isNotEmpty(companyParam.getKeywords())){
            queryWrapper.like(RiderCompany::getName, companyParam.getKeywords());
        }
        if(!Objects.isNull(companyParam.getType())){
            queryWrapper.like(RiderCompany::getType, companyParam.getType());
        }
        List<RiderCompany> list =  riderCompanyService.list(queryWrapper);
        return AjaxResult.success(list);
    }


}

